package com.bruce.httpclient;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.javafx.fxml.builder.URLBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Bruce
 */
public class TestHttpClient {
    public static void main(String[] args) throws Exception {
        tetsPost();

    }
    public static void testGetNoParams() throws IOException {
        //创建客户端对象
        HttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet("http://localhost:80/test");
        //接收响应对象
        HttpResponse response = client.execute(get);
        //获取响应体，响应数据是一个基于HTTP协议标准字符串封装的对象
        HttpEntity entity = response.getEntity();

        String responseString = EntityUtils.toString(entity, "UTF-8");

        System.out.println("服务器响应数据： " + responseString);

        client = null;
    }

    public static void testGetParams() throws URISyntaxException, IOException {
        HttpClient client = HttpClients.createDefault();
        URIBuilder builder = new URIBuilder("http://localhost/params");
//        builder.addParameter("name","bruce");
//        builder.addParameter("password", "987nsghr");
//        URI uri = builder.build();

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("name", "bruce"));
        nvps.add(new BasicNameValuePair("password", "987nsghr"));
        builder.addParameters(nvps);
        URI uri = builder.build();
        String result = EntityUtils.toString(client.execute(new HttpGet(uri)).getEntity());
        System.out.println(result);

    }


    public static void tetsPost() throws Exception{
        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://localhost:80/test");
        HttpResponse response = client.execute(post);
        System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));

        URIBuilder builder = new URIBuilder("http://localhost/params");
        builder.addParameter("name", "0484cjagw");
        builder.addParameter("password", "987nsghr");
        HttpResponse postResponse = client.execute(new HttpPost(builder.build()));
        System.out.println(EntityUtils.toString(postResponse.getEntity(), "UTF-8"));

        HttpPost bodyParamsPost = new HttpPost("http://localhost/bodyParams");
//        HttpEntity entity = new StringEntity("name=abc&password=123");
//        bodyParamsPost.setEntity(entity);
//        System.out.println(EntityUtils.toString(client.execute(bodyParamsPost).getEntity(), "UTF-8"));



//        String paramsString = "[" + u1.toString() + ", " + u2.toString() +"]";
        List<User> users = new ArrayList<>();
        users.add(new User("0484cjagw", "987nsghr",null));
        users.add(new User("bruce", "123456",null));
        ObjectMapper objectMapper = new ObjectMapper();
        String paramsString = objectMapper.writeValueAsString(users);
        System.out.println(paramsString);

        HttpEntity entity = new StringEntity(paramsString, "application/json","UTF-8");
        bodyParamsPost.setEntity(entity);
        String responseString = EntityUtils.toString(client.execute(bodyParamsPost).getEntity(), "UTF-8");
        System.out.println("responseString: "+responseString);

//        String userString = responseString.substring(1, responseString.indexOf("},") + 1);
//        System.out.println(userString);
//        User user = objectMapper.readValue(userString, User.class);
//        System.out.println(user);
//        JavaType valueType = objectMapper.getTypeFactory().constructParametricType(List.class, User.class);
//        List<User> list = objectMapper.readValue(responseString, valueType);
//        System.out.println(list);
    }
}
